package com.yt.base.controller.order;

import com.google.common.base.Splitter;
import com.yt.core.MsgBean;
import com.yt.order.api.interfaces.InstitutionService;
import com.yt.order.api.interfaces.ProductInstitutionService;
import com.yt.order.api.model.Institution;
import com.yt.order.api.model.ProductInstitution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Description
 * <p>
 * </p>
 * DATE 2017/10/12.
 *
 * @author liujunshi.
 */
@Controller
@RequestMapping("/order/product")
public class ProductInstitutionController {
    @Autowired
    private InstitutionService institutionService;
    @Autowired
    private ProductInstitutionService productInstitutionService;

    @RequestMapping("/toProductInstitution")
    public ModelAndView list(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("order/product/productInstitution");
        List<Institution> institutionList = institutionService.getIdAndName(true);
        Map<Long, List<Institution>> map = new LinkedHashMap<Long, List<Institution>>();
        List<Institution> resultList = new ArrayList<Institution>(institutionList.size());
        for(Institution institution : institutionList){
            List<Institution> subList = null;
            if(institution.getParentId() == 0){
                subList = new ArrayList<Institution>();
                subList.add(institution);
                map.put(institution.getId(), subList);
            }else{
                subList = map.get(institution.getId());
                if(subList == null) continue;
                institution.setName("|--" + institution.getName());
                subList.add(institution);
            }
        }

        Set<Map.Entry<Long, List<Institution>>> entrySet = map.entrySet();
        for(Map.Entry<Long, List<Institution>> entry : entrySet){
            resultList.addAll(entry.getValue());
        }

        mav.addObject("institutionList", resultList);
        return mav;
    }

    @RequestMapping("/saveProductInstitution")
    public @ResponseBody  Object list(
            HttpServletRequest request,
            @RequestParam("productIds") String productIds,
            @RequestParam("institutionIds") String institutionIds
            ) {
        MsgBean msgBean = new MsgBean();
        Iterator<String> pIdIt = Splitter.on(",").split(productIds).iterator();
        Iterator<String>  iIdIt = Splitter.on(",").split(institutionIds).iterator();
        while(pIdIt.hasNext()){
            while(iIdIt.hasNext()){
                ProductInstitution bean = new ProductInstitution();
                bean.setProductId(Long.parseLong(pIdIt.next()));
                bean.setInstitutionId(Long.parseLong(iIdIt.next()));
                productInstitutionService.insert(bean);
            }
        }
        return msgBean.returnMsg();
    }
}
